package com.zimmur.app.mocolink.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zimmur.app.mocolink.dao.CommonQuestionMapper;
import com.zimmur.app.mocolink.domain.model.CommonQuestion;
import com.zimmur.app.mocolink.domain.model.CommonQuestionExample;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.service.ICommonQuestionService;

@Service
public class CommonQuestionServiceImpl implements ICommonQuestionService {
 
	@Autowired
	private CommonQuestionMapper commonQuestionMapper;
	
	@Override
	public List<CommonQuestion> selectShowQuestion() {
		CommonQuestionExample example = new CommonQuestionExample();
		example.createCriteria().andIsShowEqualTo((byte)1);
		example.setOrderByClause("id asc");
		List<CommonQuestion> commonQuestions = commonQuestionMapper.selectByExampleWithBLOBs(example);
		if(commonQuestions!=null && commonQuestions.size()>0){
			for(CommonQuestion question:commonQuestions){
				String answer = question.getContent();
				if(!StringUtils.isBlank(answer)){
					System.out.println(answer);
					answer=answer.replace("\n", "<br/>");
					question.setContent(answer);
				}
			}
		}
		return commonQuestions;
	}

	@Override
	public List<CommonQuestion> selectAll() {
		CommonQuestionExample example = new CommonQuestionExample();
		example.setOrderByClause(" id desc");
		return commonQuestionMapper.selectByExampleWithBLOBs(example);
	}

	@Override
	public int update(CommonQuestion question) {
		
		return commonQuestionMapper.updateByPrimaryKeySelective(question);
	}

	@Override
	public int add(CommonQuestion question) {
		return commonQuestionMapper.insertSelective(question);
	}

	@Override
	public CommonQuestion selectById(Integer id) {
		return commonQuestionMapper.selectByPrimaryKey(id);
	}

	@Override
	public int delete(Integer questionId) {
		return commonQuestionMapper.deleteByPrimaryKey(questionId);
	}

}
